using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading.Tasks;
using System.Timers;
using UnityEngine;
using Voodoo.Sauce.Internal.Utils;

namespace Voodoo.Analytics
{
	internal static class Tracker
	{
		private const string TAG = "Tracker";

		private const int MAX_SUSPEND_SENDING_COUNT = 16;

		private static readonly string SaveFolder = Application.persistentDataPath + "/VoodooAnalyticsSDK/";

		private static readonly object CurrentFileLock = new object();

		private static string _currentFilePath;

		private static int _currentFileTimeStamp;

		private static int _currentFileEventCount;

		private static string _bundleId;

		private static IConfig _config;

		private static Timer _sendEventsTimer;

		private static ConcurrentQueue<string> _eventFilesToSend;

		private static int _currentSendingCount;

		private static int _suspendSending;

		internal static void Initialise(IConfig config, string proxyServer, string gatewayUrl)
		{
			_config = config;
			_bundleId = Application.identifier;
			AnalyticsApi.SetAnalyticsGatewayUrl(gatewayUrl);
			AnalyticsApi.ProxyServer = proxyServer;
			EnqueueEventFilesToSend();
			StartSendEventsTimer();
		}

		private static void StartSendEventsTimer()
		{
			_sendEventsTimer = new Timer(_config.GetSenderWaitIntervalSeconds() * 1000);
			_sendEventsTimer.Elapsed += delegate
			{
				try
				{
					_currentSendingCount++;
					if (_currentSendingCount >= _suspendSending)
					{
						SendEvents();
						_currentSendingCount = 0;
					}
				}
				catch (Exception exception)
				{
					AnalyticsLog.CustomLoggerLogE("Tracker", exception);
					AnalyticsLog.CustomLoggerReportE(exception);
				}
			};
			_sendEventsTimer.AutoReset = true;
			_sendEventsTimer.Enabled = true;
		}

		internal static void Start()
		{
			_sendEventsTimer?.Start();
		}

		internal static void Stop()
		{
			_sendEventsTimer?.Stop();
		}

		private static void EnqueueEventFilesToSend()
		{
		}

		private static void SendEvents()
		{
			lock (CurrentFileLock)
			{
				if (_currentFilePath != null)
				{
					_eventFilesToSend.Enqueue(_currentFilePath);
				}
				_currentFilePath = null;
			}
		}

		private static void SendFile(string filePath)
		{
		}

		private static void DeleteFile(string filePath)
		{
		}

		private static void CreateNewFile()
		{
			_currentFileTimeStamp = TimeUtils.NowAsTimeStamp();
			_currentFilePath = SaveFolder + "events_" + _currentFileTimeStamp + ".json";
			_currentFileEventCount = 0;
		}

		internal static async Task TrackEvent(VanEvent vanEvent)
		{
			if (_config == null)
			{
				AnalyticsLog.LogE("Tracker", "Can't track event: " + vanEvent.GetName() + " because the Tracker is not initialised");
			}
			else if (IsEventAuthorised(vanEvent))
			{
				SaveEvent(vanEvent);
			}
		}

		private static bool IsEventAuthorised(VanEvent vanEvent)
		{
			string[] array = _config.EnabledEvents();
			if (array.Length != 0 && !array.Contains(vanEvent.GetName()))
			{
				AnalyticsLog.Log("Tracker", "Event ignored: " + vanEvent.GetName());
				return false;
			}
			return true;
		}

		private static async Task SaveEvent(VanEvent vanEvent)
		{
			lock (CurrentFileLock)
			{
				if (string.IsNullOrEmpty(_currentFilePath))
				{
					CreateNewFile();
				}
				else if (_currentFileEventCount >= _config.GetMaxNumberOfEventsPerFile() || TimeUtils.NowAsTimeStamp() > _currentFileTimeStamp + _config.GetFileIntervalInSeconds())
				{
					_eventFilesToSend.Enqueue(_currentFilePath);
					CreateNewFile();
				}
			}
		}
	}
}
